package com.huayi.order.controller;

import com.huayi.order.service.SalesManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/sales_man")
public class SalesManController {
    @Autowired
    private SalesManService salesManService;

    @RequestMapping("/list")
    public String list(Model model, HttpServletRequest request) throws Exception {
        String trueName="";
        if (!StringUtils.isEmpty(request.getParameter("trueName"))) {
            trueName = request.getParameter("trueName");
            model.addAttribute("trueName",trueName);
        }
        String mobile="";
        if (!StringUtils.isEmpty(request.getParameter("mobile"))) {
            mobile = request.getParameter("mobile");
            model.addAttribute("mobile",mobile);
        }

        model.addAttribute("menuId", "sales_man_list");

        var list = salesManService.getList(trueName,mobile);
        model.addAttribute("list",list);
        model.addAttribute("totalSize",list.size());

        return "sales_man_list";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Model model) throws Exception {
        model.addAttribute("menuId", "sales_man_list");

        return "sales_man_add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addPOST(Model model,HttpServletRequest request) throws Exception {
        String trueName = request.getParameter("trueName");
        String mobile = request.getParameter("mobile");
        String summary = request.getParameter("summary");
        salesManService.add(trueName,mobile,summary);

        model.addAttribute("menuId", "sales_man_list");

        return "redirect:/sales_man/list";
    }
}
